package com.zsc.wxm.ysh.anjo.es.task;

import com.zsc.wxm.ysh.anjo.es.crawler.DataCrawlerProcessor;
import com.zsc.wxm.ysh.anjo.es.crawler.DataPipeLine;
import com.zsc.wxm.ysh.anjo.es.feign.ResourcesService;
import com.zsc.wxm.ysh.anjo.es.service.UploadService;
import com.zsc.wxm.ysh.anjo.service.base.constant.CityAreaConstant;
import com.zsc.wxm.ysh.anjo.service.base.constant.CrawlerConstant;
import com.zsc.wxm.ysh.anjo.service.base.constant.OtherConstant;
import com.zsc.wxm.ysh.anjo.service.base.enums.resources.CityType;
import com.zsc.wxm.ysh.anjo.service.base.model.es.dto.CrawlerDataDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Spider;

import javax.annotation.Resource;
import java.io.File;
import java.util.*;

/**
 * 描述： 爬虫定时任务
 *
 * @author playmaker
 * @since 2021/2/21 17:26
 */
@Slf4j(topic = "爬虫定时任务")
@Service
public class PullDataToEsByCrawlerTask {

    @Resource
    private UploadService uploadService;

    @Resource
    private ResourcesService resourcesService;


    private DataCrawlerProcessor dataCrawlerProcessor = new DataCrawlerProcessor();

    /**
     * 区域
     */
    private static Map<String ,List<String>> areas;

    /**
     * 区域名称
     */
    private static Map<String, List<String>> areaNames;

    /**
     * url渠道
     */
    private static List<String> crawlerUrls;

    static {
        crawlerUrls = new ArrayList<>();
        areas = new HashMap<>();
        areaNames = new HashMap<>();
        //链接网渠道
        crawlerUrls = Arrays.asList(CrawlerConstant.BJ_LIAN_JIA_URL, CrawlerConstant.GZ_LIAN_JIA_URL,
                CrawlerConstant.SH_LIAN_JIA_URL, CrawlerConstant.SZ_LIAN_JIA_URL,
                CrawlerConstant.ZS_LIAN_JIA_URL);
        //城市区域
        for (String url : crawlerUrls) {
            if (url.contains(CityType.BJ.getDesc())) {
                areas.put(url, Arrays.asList(CityAreaConstant.BJ_AREA.split(",")));
                areaNames.put(url, Arrays.asList(CityAreaConstant.BJ_AREA_NAME.split(",")));
            } else if (url.contains(CityType.GZ.getDesc())) {
                areas.put(url, Arrays.asList(CityAreaConstant.GZ_AREA.split(",")));
                areaNames.put(url, Arrays.asList(CityAreaConstant.GZ_AREA_NAME.split(",")));
            } else if (url.contains(CityType.SH.getDesc())) {
                areas.put(url, Arrays.asList(CityAreaConstant.SH_AREA.split(",")));
                areaNames.put(url, Arrays.asList(CityAreaConstant.SH_AREA_NAME.split(",")));
            } else if (url.contains(CityType.SZ.getDesc())) {
                areas.put(url, Arrays.asList(CityAreaConstant.SZ_AREA.split(",")));
                areaNames.put(url, Arrays.asList(CityAreaConstant.SZ_AREA_NAME.split(",")));
            } else if (url.contains(CityType.ZS.getDesc())) {
                areas.put(url, Arrays.asList(CityAreaConstant.ZS_AREA.split(",")));
                areaNames.put(url, Arrays.asList(CityAreaConstant.ZS_AREA_NAME.split(",")));
            }
        }
    }

    //@Scheduled(cron = "0 0 0/5 * * ?")
    public void execute() {
        long start = System.currentTimeMillis();
        doExecute();
        log.info("爬虫定时任务执行完成，消耗的时间为{}ms", System.currentTimeMillis() - start);
    }

    /**
     * 任务执行
     */
    private void doExecute() {
        for (String url : crawlerUrls) {
            dataCrawlerProcessor.setChannelUrl(url);
            List<String> areaList = areas.get(url);
            List<String> areaNameList = areaNames.get(url);
            for (int i = 0;i < areaList.size();i ++) {
                //拉取前先删除data.json
                File file = new File(OtherConstant.JSON_DATA_PATH + "data.json");
                file.delete();
                //删除图片文件夹
                deleteFiles(new File(OtherConstant.IMAGE_SAVE_PATH));
                pull(url, areaList.get(i));
                CrawlerDataDTO crawlerDataDTO = new CrawlerDataDTO();
                crawlerDataDTO.setArea(areaNameList.get(i));
                crawlerDataDTO.setAddress(areaNameList.get(i));
                crawlerDataDTO.setYear("2007");
                crawlerDataDTO.setDevelopers(areaNameList.get(i) + "开发商");
                String[] cityNameAndProvince = getCityNameAndProvince(url);
                crawlerDataDTO.setCity(cityNameAndProvince[0]);
                crawlerDataDTO.setProvince(cityNameAndProvince[1]);
                crawlerDataDTO.setType("民用建筑");
                crawlerDataDTO.setPropertyCost("100");
                log.info("resourcesService:{}",resourcesService);
                //保存到数据库
                resourcesService.addData(crawlerDataDTO);
                //爬取的图片上传到oss
                uploadService.uploadHouseImage();
            }
        }
        //导入到es中
        resourcesService.batchAddHouseData();
    }

    /**
     * 爬取数据
     *
     * @param url  爬取地址
     * @param area 区域
     */
    private void pull(String url, String area) {
        Spider.create(dataCrawlerProcessor)
                //从"链家网"开始抓
                .addUrl(url + area + "/")
                //开启1个线程抓取
                .addPipeline(new DataPipeLine())
                .thread(3)
                .run();
    }

    /**
     * 获取城市名和省份
     *
     * @param url 爬虫路径
     * @return String[]
     */
    private String[] getCityNameAndProvince(String url) {
        String[] cityNameAndProvince = new String[2];
        if (url.contains(CityType.BJ.getDesc())) {
            cityNameAndProvince[0] = CityType.BJ.getName();
            cityNameAndProvince[1] = CityType.BJ.getProvince();
            return cityNameAndProvince;
        } else if (url.contains(CityType.GZ.getDesc())) {
            cityNameAndProvince[0] = CityType.GZ.getName();
            cityNameAndProvince[1] = CityType.GZ.getProvince();
            return cityNameAndProvince;
        } else if (url.contains(CityType.SH.getDesc())) {
            cityNameAndProvince[0] = CityType.SH.getName();
            cityNameAndProvince[1] = CityType.SH.getProvince();
            return cityNameAndProvince;
        } else if (url.contains(CityType.SZ.getDesc())) {
            cityNameAndProvince[0] = CityType.SZ.getName();
            cityNameAndProvince[1] = CityType.SZ.getProvince();
            return cityNameAndProvince;
        } else if (url.contains(CityType.ZS.getDesc())) {
            cityNameAndProvince[0] = CityType.ZS.getName();
            cityNameAndProvince[1] = CityType.ZS.getProvince();
            return cityNameAndProvince;
        }
        return cityNameAndProvince;
    }

    /**
     * 删除整个文件夹：
     * 当为目录时，递归调用删除子文件
     *                      最后删除自己
     * 当为文件执行最后一行，直接删除
     *
     * @param file 目录
     *
     */
    private void deleteFiles(File file){
        if (file.isDirectory()) {
            File[] files=file.listFiles();
            if (files != null && files.length > 0) {
                for (File value : files) {
                    if (value.isDirectory()) {
                        deleteFiles(value);
                    } else {
                        if (value.delete()) {
                            log.info("文件删除成功，文件名称{}", value.getName());
                        }
                    }
                }
            }
        }
        //最外层文件
        file.delete();
        log.info("文件删除完成...");
    }


}
